package com.zdy.worldtion.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.zdy.worldtion.R;
import com.gyf.barlibrary.ImmersionBar;
import androidx.annotation.Nullable;


public abstract class BaseInitActivity extends BaseActivity {

    private ImmersionBar mImmersionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initSystemFit();
        initIntent(getIntent());
        initView(savedInstanceState);
        initListener();
        initData();
        mImmersionBar = ImmersionBar.with(this).statusBarColor(R.color.white).keyboardEnable(true).
                statusBarDarkFont(true, 0.2f).fitsSystemWindows(true);
        getmImmersionBar().reset().statusBarColor(R.color.white).statusBarDarkFont(true, 0.2f).init();
        mImmersionBar.init();
        hideKeyboard();
    }

    public void hideSoftInput(Context context, View view) {
        InputMethodManager imm =
                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        hideKeyboard();
    }

    public ImmersionBar getmImmersionBar() {
        return mImmersionBar;
    }

    protected void initSystemFit() {
        setFitSystemForTheme(true);
    }

    /**
     * hide keyboard
     */
    public void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm!=null&&getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null){
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){

        if(null != this.getCurrentFocus()){
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }

        return super.onTouchEvent(event);
    }

    /**
     * get layout id
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * init intent
     *
     * @param intent
     */
    protected void initIntent(Intent intent) {
    }

    /**
     * init view
     *
     * @param savedInstanceState
     */
    protected void initView(Bundle savedInstanceState) {

    }

    /**
     * init listener
     */
    protected void initListener() {
    }

    /**
     * init data
     */
    protected void initData() {
    }
}
